<?php

// +----------------------------------------------------------------------
// | 版权所有 2017-2027 云南环拓科技有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.yn123.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// | 注意：  这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用,
// |        不允许对程序代码以任何形式任何目的的再发布。
// +----------------------------------------------------------------------
// | Author: feraner <feraner@163.com> 
// | Date: 2019/11/12
// +----------------------------------------------------------------------

namespace app\common\services;
use app\common\facade\Arr;
use think\Db;
use think\Exception;

/**
 *
 * Class PublishJobComputerService
 * @package app\common\services
 * @author feraner<feraner@163.com>
 * @date 2019/11/12
 */
class PublishJobComputerService
{


    /**
     * 计算出发布该兼职需要 多少发布豆
     * @param $jobInfo
     *
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function needBean($jobInfo)
    {
        try {
            //1： 获取所属代理商
            $agencyInfo = $this->__getAgencyInfo($jobInfo);
            if ($agencyInfo) {
                //2: 代理商兼职发布豆配置信息
                $agencyBeanInfo = Db::name('bean')
                    ->where('aid', Arr::get($agencyInfo, 'id'))
                    ->find() ?: [];

                //3: 兼职发布豆信息 [*代理商未配置读取系统配置]
                //发布一条兼职消耗几个发布豆
                $basePublishJobNeedBean = Arr::get($agencyBeanInfo, 'need_bean', (sysconf('publish_job_need_bean') ?: 0));
                //一条兼职基础人数值
                $publishJobBasePeople = Arr::get($agencyBeanInfo, 'base_people', (sysconf('publish_job_base_people') ?: 0));
                //每增加一名招聘人员需要几个发布豆
                $publishJobAddOneNeedBean = Arr::get($agencyBeanInfo, 'add_bean', (sysconf('publish_job_add_one_need_bean') ?: 0));
                //消耗一个发布豆送几个积分
                $usedBeanGiveCredits = sysconf('used_bean_give_credits') ?: 0;

                $needBean = $basePublishJobNeedBean;

                $addBeanPeople = intval(Arr::get($jobInfo, 'need_worker', 0) - $publishJobBasePeople);
                if ($addBeanPeople < 0) {
                    $addBeanPeople = 0;
                }
                $needBean += ($addBeanPeople * $publishJobAddOneNeedBean);
                $giveCredits = ($needBean * $usedBeanGiveCredits);
                return [
                    'needBean'      => $needBean ?: 0,
                    'giveCredits'   => $giveCredits ?: 0,
                    'baseBean'      => $basePublishJobNeedBean,
                    'baseGiveCredits' => ($basePublishJobNeedBean * $usedBeanGiveCredits),
                    'addOneNeedBean' => $publishJobAddOneNeedBean,
                    'oneBeanGiveCredits' => $usedBeanGiveCredits
                ];
            }
            throw new \Exception('未找到代理商');
        } catch (\Exception $e) {
            throw $e;
        }
    }


    /**
     * 依据代理商获取地区ID
     * @param $jobInfo
     *
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function __getAgencyInfo($jobInfo)
    {
        //这里的jobInfo 的地区可能有两种格式 1：添加 2：来自数据库
        $provincesId = Arr::get($jobInfo, 'provincesId', Arr::get($jobInfo, 'provinces_id', 0));
        $cityId = Arr::get($jobInfo, 'cityId', Arr::get($jobInfo, 'city_id', 0));
        $countyId = Arr::get($jobInfo, 'countyId', Arr::get($jobInfo, 'county_id', 0));

         $agencyInfo = Db::name('system_agent_user')
             ->where('provinces_id', $provincesId)
             ->where('city_id', $cityId)
             ->where('status', 1)
             ->where('is_deleted', 0)
             ->where('pid', 0)
             ->find() ?: [];

         return $agencyInfo;
    }







}
